package com.welink.cmp.meatdata.command;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.metadata.RouterDaoService;
import com.welink.cmp.dao.metadata.dataobject.RouterDO;
import com.welink.cmp.dto.metadata.MetadataIdListQry;
import com.welink.cmp.dto.metadata.router.CreateRouterAddCmd;
import com.welink.cmp.dto.metadata.router.clientobject.RouterCO;
import com.welink.cmp.dto.metadata.router.query.RouterListByPageQry;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Router命令执行类
 * 负责处理Router相关的业务逻辑和数据操作
 */
@Component
public class RouterCmdExe {

    @Autowired
    private RouterDaoService routerDaoService;

    /**
     * 保存Router信息
     *
     * @param addCmd 添加Router的命令对象
     * @return 新增Router的ID
     */
    public Long save(CreateRouterAddCmd addCmd) {
        RouterDO routerDO = new RouterDO();
        BeanUtils.copyProperties(addCmd, routerDO);
        if (addCmd.isNeedsOperator()){
            routerDO.setCreateBy(addCmd.getOperator());
        }
        routerDaoService.save(routerDO);
        return routerDO.getId();
    }

    /**
     * 更新Router信息
     *
     * @param updDo 更新Router的命令对象
     * @return 更新是否成功
     */
    public Boolean update(RouterDO updDo) {
        return routerDaoService.updateById(updDo);
    }

    /**
     * 根据ID查询Router信息
     *
     * @param id Router ID
     * @return Router业务对象
     */
    public RouterCO queryById(Long id) {
        RouterDO routerDO = routerDaoService.getById(id);
        RouterCO routerCO = new RouterCO();
        BeanUtils.copyProperties(routerDO, routerCO);
        return routerCO;
    }

    /**
     * 列表查询Router信息
     *
     * @param listQry 列表查询条件
     * @return Router业务对象列表
     */
    public List<RouterCO> listQuery(MetadataIdListQry listQry) {
        LambdaQueryWrapper<RouterDO> wrapper = new LambdaQueryWrapper<>();
        // 根据列表查询条件设置查询条件

        final List<Long> idList = listQry.getIdList();
        List<RouterDO> routerDOList = routerDaoService.listByIds(idList);
        return GenericConvertor.toCOList(routerDOList, RouterCO.class);
    }

    /**
     * 分页查询Router信息
     *
     * @param pageQry 分页查询条件
     * @return 分页响应，包括Router列表和分页信息
     */
    public Page<RouterDO> pageQuery(RouterListByPageQry pageQry) {
        return routerDaoService.pageQry(pageQry);
    }
}